package org.kele.sc2020.consumer.controller;

import cn.hutool.core.util.RandomUtil;
import io.github.resilience4j.retry.annotation.Retry;
import lombok.extern.slf4j.Slf4j;
import org.kele.sc2020.common.dto.KeleResult;
import org.kele.sc2020.consumer.rpc.ScProviderRPC;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
@RestController
public class ConsumController {

    @Resource
    ScProviderRPC scProviderRPC;


    @GetMapping("/get-from-p")
    public String getFromprovider(HttpServletRequest request) {

        request.setAttribute("kele", "i am xuekele " + RandomUtil.randomInt());

        return scProviderRPC.getFromProvider("xukele");
    }

    @Retry(name = "default")
    @GetMapping("/test-retry")
    public KeleResult<Object> testRetryRpc() {
        return scProviderRPC.testRetry();
    }


    public static final BlockingQueue<Object> BLOCKING_QUEUE = new LinkedBlockingQueue<>(1000);

    @GetMapping("/push-ele")
    public boolean pushElement() {
        return BLOCKING_QUEUE.offer("kele:" + RandomUtil.randomString(10));
    }
}
